package com.example.chinesechess.domain.model

/**
 * Represents a Chinese chess piece
 * @param type The type of chess piece
 * @param side The side (color) of the piece
 * @param position The position on the board
 */
data class ChessPiece(
    val type: PieceType,
    val side: Side,
    val position: Position
) {
    /**
     * Check if this piece can capture another piece
     */
    fun canCapture(other: ChessPiece): Boolean {
        return this.side != other.side
    }
    
    /**
     * Get the Chinese character representation of this piece
     */
    fun getDisplayName(): String {
        return when (side) {
            Side.RED -> when (type) {
                PieceType.GENERAL -> "帅"
                PieceType.ADVISOR -> "士"
                PieceType.ELEPHANT -> "象"
                PieceType.HORSE -> "马"
                PieceType.CHARIOT -> "车"
                PieceType.CANNON -> "炮"
                PieceType.SOLDIER -> "兵"
            }
            Side.BLACK -> when (type) {
                PieceType.GENERAL -> "将"
                PieceType.ADVISOR -> "士"
                PieceType.ELEPHANT -> "相"
                PieceType.HORSE -> "马"
                PieceType.CHARIOT -> "车"
                PieceType.CANNON -> "炮"
                PieceType.SOLDIER -> "卒"
            }
        }
    }
}

/**
 * Represents a position on the board
 * @param x Column (0-8)
 * @param y Row (0-9)
 */
data class Position(val x: Int, val y: Int) {
    fun isValid(): Boolean {
        return x in 0..8 && y in 0..9
    }
    
    /**
     * Check if this position is in the palace area
     */
    fun isInPalace(side: Side): Boolean {
        val xInPalace = x in 3..5
        return when(side) {
            Side.RED -> xInPalace && y in 7..9
            Side.BLACK -> xInPalace && y in 0..2
        }
    }
    
    /**
     * Check if this position is across the river relative to the side
     */
    fun isAcrossRiver(side: Side): Boolean {
        return when (side) {
            Side.RED -> y < 5
            Side.BLACK -> y > 4
        }
    }
}

/**
 * Side/color of chess pieces
 */
enum class Side {
    RED, BLACK;
    
    fun opposite(): Side {
        return if (this == RED) BLACK else RED
    }
}

/**
 * Types of Chinese chess pieces
 */
enum class PieceType {
    GENERAL,    // 将/帅
    ADVISOR,    // 士
    ELEPHANT,   // 象/相
    HORSE,      // 马
    CHARIOT,    // 车
    CANNON,     // 炮
    SOLDIER     // 兵/卒
} 